/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tolls;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import model.DBManager;

/**
 *
 * @author Αντώνης Μιχαηλίδης
 */
public class SearchCollectionHistoryForm extends javax.swing.JFrame {
    
EntityManager em;
String strCardId;
String strRegNum;
String strStartDate;
String strEndDate;

    /**
     * Creates new form SearchCollectionHistoryForm
     */

    public SearchCollectionHistoryForm() {
        em=DBManager.em;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CardIdTextField = new javax.swing.JTextField();
        CardIdLabel = new javax.swing.JLabel();
        RegNumLabel = new javax.swing.JLabel();
        RegNumTextField = new javax.swing.JTextField();
        StartDateLabel = new javax.swing.JLabel();
        StartDateTextField = new javax.swing.JTextField();
        EndDateLabel = new javax.swing.JLabel();
        EndDateTextField = new javax.swing.JTextField();
        SearchButton = new javax.swing.JButton();
        ReturnButton = new javax.swing.JButton();
        DateFormatLabel1 = new javax.swing.JLabel();
        DateFormatLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Αναζήτηση Ιστορικού Διελεύσεων");

        CardIdTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CardIdTextFieldActionPerformed(evt);
            }
        });

        CardIdLabel.setText("Αριθμός Κάρτας :");

        RegNumLabel.setText("Αριθμός Κυκλοφορίας :");

        StartDateLabel.setText("Ημερομηνία  Από:");

        EndDateLabel.setText("Ημερομηνία Έως :");

        SearchButton.setText("Αναζήτηση");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        ReturnButton.setText("Επιστροφή");
        ReturnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReturnButtonActionPerformed(evt);
            }
        });

        DateFormatLabel1.setText("(dd/MM/yyyy)");

        DateFormatLabel2.setText("(dd/MM/yyyy)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RegNumLabel)
                    .addComponent(StartDateLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(CardIdLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(EndDateLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SearchButton, javax.swing.GroupLayout.Alignment.LEADING))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(RegNumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(CardIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(StartDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(EndDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DateFormatLabel1)
                            .addComponent(DateFormatLabel2))
                        .addContainerGap(76, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ReturnButton)
                        .addGap(95, 95, 95))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CardIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CardIdLabel))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RegNumLabel)
                    .addComponent(RegNumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StartDateLabel)
                    .addComponent(StartDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DateFormatLabel1))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EndDateLabel)
                    .addComponent(EndDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DateFormatLabel2))
                .addGap(97, 97, 97)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SearchButton)
                    .addComponent(ReturnButton))
                .addGap(44, 44, 44))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CardIdTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CardIdTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CardIdTextFieldActionPerformed

    private void ReturnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReturnButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_ReturnButtonActionPerformed

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed
        // TODO add your handling code here:
        
        DateFormat df1=new SimpleDateFormat("dd/MM/yyyy");  //Μορφή ημερομηνίας
   
    //Αποθηκεύουμε τις τιμές που εισήγαγε ο χρήστης στα textfield
    strCardId=CardIdTextField.getText();        
    strRegNum=RegNumTextField.getText();
    strStartDate=StartDateTextField.getText();
    strEndDate=EndDateTextField.getText();
  
    //Έλεγχος αν ο χρήστης δεν έχει εισάγει καμία τιμή
    if (strCardId.isEmpty() && strRegNum.isEmpty() && strStartDate.isEmpty() && strEndDate.isEmpty()){
        JOptionPane.showMessageDialog(this,"Η Φόρμα Αναζήτησης είναι κενή. Παρακαλώ συμπληρώστε τουλάχιστον ένα πεδίο.");
    }
    else{    
        if (strStartDate.isEmpty()){        //Αν δεν έχει εισαχθεί ημ/νια έναρξης αναζήτησης, τη θέτω ίση με το epoch time 
            strStartDate="01/01/1900";
        }
        if (strEndDate.isEmpty()){          //Αν δεν έχει εισαχθεί ημ/νια τέλους αναζήτησης, τη θέτω ίση με τη σημερινή
            Date today=new Date();
            strEndDate=df1.format(today);
        }
    
        if (strCardId.isEmpty()){        //Αν δεν έχει εισαχθεί Αριθμός Κάρτας, τότε το θέτω ίσο με *
            strCardId="*";
        }
    
        if (strRegNum.isEmpty()){        //Αν δεν έχει εισαχθεί Αριθμός Κυκλοφορίας, τότε το θέτω ίσο με *
            strRegNum="*";
        }
     
    }
        //Πέρασμα παραμέτρων στη φόρμα εμφάνισης lphf
        ListCollectionHistoryForm lchf=new ListCollectionHistoryForm(strCardId,strRegNum,strStartDate,strEndDate);
        lchf.setVisible(true);
    }//GEN-LAST:event_SearchButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SearchCollectionHistoryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SearchCollectionHistoryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SearchCollectionHistoryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SearchCollectionHistoryForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SearchCollectionHistoryForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CardIdLabel;
    private javax.swing.JTextField CardIdTextField;
    private javax.swing.JLabel DateFormatLabel1;
    private javax.swing.JLabel DateFormatLabel2;
    private javax.swing.JLabel EndDateLabel;
    private javax.swing.JTextField EndDateTextField;
    private javax.swing.JLabel RegNumLabel;
    private javax.swing.JTextField RegNumTextField;
    private javax.swing.JButton ReturnButton;
    private javax.swing.JButton SearchButton;
    private javax.swing.JLabel StartDateLabel;
    private javax.swing.JTextField StartDateTextField;
    // End of variables declaration//GEN-END:variables
}
